package cl.ufro.dis.protocolos;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

@SuppressWarnings("deprecation")
public class ConexionWS {

	public static String Servicio(String methodName){
		String METHOD_NAME = methodName;
		String NAMESPACE = "http://ws.sgmas.openmrsapi/";
		String URL = "http://londres.ceisufro.cl:8080/SGMAS/services/Paciente";
	
		return Servicio(METHOD_NAME, NAMESPACE, URL);
	}
	
	public static String Servicio(String method_name,String namespace, String url){

		String METHOD_NAME = method_name;
		String NAMESPACE = namespace;
		String URL = url;
		String SOAP_ACTION = namespace + method_name;
			
	    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
	    Request.addProperty("run", "8877252-0");
	    Request.addProperty("username", "4tHy68JOas22L90MBV890FHGVDGJKIkh4g67");
	    Request.addProperty("token", "   4tHy68JOL90MBV890FHGVDG12233JKIkh4g67");
	    Request.addProperty("tokenName", "token_pin");

	    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
	    soapEnvelope.dotNet = true;
	    soapEnvelope.setOutputSoapObject(Request);

	    AndroidHttpTransport aht = new AndroidHttpTransport(URL);
	    //HttpTransportSE aht = new HttpTransportSE(URL);

	    try
	    {
	    	aht.call(SOAP_ACTION, soapEnvelope);
	        SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
	        String datosJsonString = Encriptacion.Decrypt64(resultString.toString(),"wsxedcrfv");
	        return datosJsonString;
	    }
	    catch (Exception e)
	    {
	    	e.printStackTrace();
		    // en caso de no tener acceso al web service
		    String var = new String("CFoMAEFIUkI1Q0pdSEEAExgiSVpdXFRFVEMyXkhHSEEcCRtiAR1HXkExKjNNNjYxLS0zRjJFP1gmJTE/IzgiX1oEFAYeCh9kHCcVRllQITdSMDEkRk9QBwZlHxQMAAwtC1Q6USggwrUiUEpUcxYACkZZUCATbRYWDAoMUEpUZhYbDQVBSERGNFxIU0tSS1NPIl9aDAAgHQsDbhJaX1FRRUpUYxwVEAoCUFxUTDItMSUxPURaIhcRFwEAEQ8ZblFCRylNICkyUjo/MCE5UlVGNlM0JDE3MzQ5Il9aCQ0QBgc3dAcRBxEXF0RMWwhaCwUOF0RMIicdCQEFHQgZIl9aEwUPBwNUOlFaSUYXGxYyYQcZR15SD0oNIh0ZCAFBSEQ7YRoUR0hBBAcadRZaX0ZBXkQCaQM8BBACUFxHfV8DRwoCHwNUOlEqEApBXkQAYR8NAEZZUF5ON0RKUFZOQkRaIgcRFSACBgdUOkIFSR9BHAcbZVFCRzQRFxAfcxoXC0ZPUBAXbAYdR15BQFVULFEMDBQnExIXIklLGEgYUAgXbRZaX0YzAAMAaQARCgpBXkQAYR8NAEZZUCA5TjIrJEROUiRULFEMDBQnExIXIklKGEgYUAgXbRZaX0Y2AAQXbhwqEBYCHkRaIgUZCREGUFxUNUBaSUYXGxYyYQcZR15QD0oNIh0ZCAFBSEQjchEZCwsxBxQXbFFURxICHhMTIklaMDYhMyg5Il9aEQ0TNgcCYVFCVxlPCUQYYR4dR15BNxIYaRJaSUYVEwoDZVFCR1FUUEpUdBoIIQUXE0RMMw5UHkYNEwsTIklaIBANGwdULFEOBAgWF0RMIiAxK0QiIiM6TDo8KjdBXkQCaQM8BBACUFxEfV8DRwoCHwNUOlE9EQoKEzYTchAdFQcKHQhULFEOBAgWF0RMIkZBR0hBBg8GRBIMBEZZQRtae1EWBAkGUFxURQcWDAUzFxQVZQMbDAsNUEpUdhIUEAFBSEQ1SDo0ICosUEpUdBoIIQUXE0RMMg4lGA==");
	        String datosJsonString = Encriptacion.Decrypt64(var,"wsxedcrfv");
	        datosJsonString = datosJsonString.replaceAll("\":\"","\":\"-FALLO WS-"); 
		    return datosJsonString;
	    }
	}
}
